package com.yuandian.customer.perception.service.impl;

import com.yuandian.api.customer_perception.po.TopBaseParams;
import com.yuandian.api.customer_perception.vo.AvgTakeTimeTopNVo;
import com.yuandian.api.customer_perception.vo.TopAnalysisOverviewVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.customer.perception.mapper.SystemTopAnalysisMapper;
import com.yuandian.customer.perception.service.ISystemTopAnalysisService;
import com.yuandian.enums.TableNameEnum;
import com.yuandian.po.MultipleTableInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 类说明: 全系统TOP 分析服务实现类
 *
 * @Date 2021/8/19 9:55
 * @Author User
 */
@Service
public class SystemTopAnalysisServiceImpl implements ISystemTopAnalysisService {

    @Autowired
    private SystemTopAnalysisMapper systemTopAnalysisMapper;

    @Override
    public R<Object> queryTopAnalysisOverview(TopBaseParams params) {

		// 获取表信息集
		MultipleTableInfo info = new MultipleTableInfo();
		info.setTableName(TableNameEnum.KHGZ_BUSI_SYS_DETAIL_STAT.getName());
		List<MultipleTableInfo> multipleTableInfoList = Collections.singletonList(info);
		params.setMultipleTableInfoList(multipleTableInfoList);

        // 查询受理量
        List<TopAnalysisOverviewVo> complete = systemTopAnalysisMapper.queryTopComplete(params);
        // 查询失败量
        List<TopAnalysisOverviewVo> fail = systemTopAnalysisMapper.queryTopFail(params);
        // 查询操作耗时
        List<TopAnalysisOverviewVo> visitTime = systemTopAnalysisMapper.queryTopAvgVisitTime(params);
        // 查询响应耗时
        List<TopAnalysisOverviewVo> responseTime = systemTopAnalysisMapper.queryTopAvgResponseTime(params);

        return R.ok(TopAnalysisOverviewVo.build(complete, fail, visitTime, responseTime, null));
    }


    
    @Override
    public R<Object> queryCompleteTopN(TopBaseParams params) {

		// 获取表信息集
		MultipleTableInfo info = new MultipleTableInfo();
		info.setTableName(TableNameEnum.KHGZ_BUSI_SYS_DETAIL_STAT.getName());
		List<MultipleTableInfo> multipleTableInfoList = Collections.singletonList(info);
		params.setMultipleTableInfoList(multipleTableInfoList);


		// 查询当前
        List<TopAnalysisOverviewVo> complete = systemTopAnalysisMapper.queryTopComplete(params);

        List<Integer> systemIds = new ArrayList<>();
        complete.forEach(item -> systemIds.add(Integer.parseInt(item.getCompleteId())));

        // 查询历史峰值和谷值
        List<TopAnalysisOverviewVo> historyComplete = new ArrayList<>();
        if (systemIds.size() > 0) {
            List<TopAnalysisOverviewVo> tmp = systemTopAnalysisMapper.queryHistoryCompleteTop(systemIds);
            if (tmp.size() > 0) {
                historyComplete.addAll(tmp);
            }
        }

        Map<String, TopAnalysisOverviewVo> temp = new HashMap<>();
        historyComplete.forEach(item -> temp.put(item.getCompleteId(), item));

        complete.forEach(item -> {
            TopAnalysisOverviewVo hisVo = temp.get(item.getCompleteId());
            if (null != hisVo) {
                item.setMinCompleteCount(hisVo.getMinCompleteCount());
                item.setMaxCompleteCount(hisVo.getMaxCompleteCount());
            }
        });


        complete.forEach(TopAnalysisOverviewVo::translateSystem);
        return R.ok(complete);
    }



    
    @Override
    public R<Object> queryFailTopN(TopBaseParams params) {
		// 获取表信息集
		MultipleTableInfo info = new MultipleTableInfo();
		info.setTableName(TableNameEnum.KHGZ_BUSI_SYS_DETAIL_STAT.getName());
		List<MultipleTableInfo> multipleTableInfoList = Collections.singletonList(info);
		params.setMultipleTableInfoList(multipleTableInfoList);

		// 查询当前
        List<TopAnalysisOverviewVo> fail = systemTopAnalysisMapper.queryTopFail(params);

        List<Integer> systemIds = new ArrayList<>();
        fail.forEach(item -> systemIds.add(Integer.parseInt(item.getFailId())));

        // 查询历史峰值和谷值
        List<TopAnalysisOverviewVo> historyFail = new ArrayList<>();
        if (systemIds.size() > 0) {
            List<TopAnalysisOverviewVo> tmp = systemTopAnalysisMapper.queryHistoryFailTop(systemIds);
            if (tmp.size() > 0) {
                historyFail.addAll(tmp);
            }
        }

        Map<String, TopAnalysisOverviewVo> temp = new HashMap<>();
        historyFail.forEach(item -> temp.put(item.getFailId(), item));

        fail.forEach(item -> {
            TopAnalysisOverviewVo hisVo = temp.get(item.getFailId());
            if (null != hisVo) {
                item.setMinFailCount(hisVo.getMinFailCount());
                item.setMaxFailCount(hisVo.getMaxFailCount());
            }
        });

        fail.forEach(TopAnalysisOverviewVo::translateSystem);
        return R.ok(fail);
    }


    
    @Override
    public R<Object> queryAvgVisitTimeTopN(TopBaseParams params) {
		// 获取表信息集
		MultipleTableInfo info = new MultipleTableInfo();
		info.setTableName(TableNameEnum.KHGZ_BUSI_SYS_DETAIL_STAT.getName());
		List<MultipleTableInfo> multipleTableInfoList = Collections.singletonList(info);
		params.setMultipleTableInfoList(multipleTableInfoList);


		// 升序
        params.setOrderBy("asc");
        List<TopAnalysisOverviewVo> ascVisitTime = systemTopAnalysisMapper.queryTopAvgVisitTime(params);
        ascVisitTime.forEach(TopAnalysisOverviewVo::translateSystem);

        // 降序
        params.setOrderBy("desc");
        List<TopAnalysisOverviewVo> descVisitTime = systemTopAnalysisMapper.queryTopAvgVisitTime(params);
        descVisitTime.forEach(TopAnalysisOverviewVo::translateSystem);

        return R.ok(AvgTakeTimeTopNVo.build(ascVisitTime, descVisitTime));
    }


    
    @Override
    public R<Object> queryAvgResponseTimeTopN(TopBaseParams params) {

		// 获取表信息集
		MultipleTableInfo info = new MultipleTableInfo();
		info.setTableName(TableNameEnum.KHGZ_BUSI_SYS_DETAIL_STAT.getName());
		List<MultipleTableInfo> multipleTableInfoList = Collections.singletonList(info);
		params.setMultipleTableInfoList(multipleTableInfoList);


		// 升序
        params.setOrderBy("asc");
        List<TopAnalysisOverviewVo> ascResponseTime = systemTopAnalysisMapper.queryTopAvgResponseTime(params);
        ascResponseTime.forEach(TopAnalysisOverviewVo::translateSystem);

        // 降序
        params.setOrderBy("desc");
        List<TopAnalysisOverviewVo> descResponseTime = systemTopAnalysisMapper.queryTopAvgResponseTime(params);
        descResponseTime.forEach(TopAnalysisOverviewVo::translateSystem);

        return R.ok(AvgTakeTimeTopNVo.build(ascResponseTime, descResponseTime));
    }

}
